package com.novaed.game.backend;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.novaed.game.backend.Paths;
import com.novaed.game.backend.Player;


public class Profile extends Player {

	public Profile(String User, String PID) {
		super(User, PID);
	}
	
	public Profile() {}

	private BufferedReader br;
	private ArrayList<String> arrayOfScores;

	public File getScoreSheet() {
		
		File uScoreSheet = new File(Paths.getProfilePath() + Integer.valueOf(getOnlineUser()) + ".txt");
		
		try {
			if (!uScoreSheet.exists()){

				System.err.println(getUserPID() + ".txt does not exist!");
				System.out.println("Writing " + getUserPID() + ".txt");

				newScoreSheet(getUserPID());

				//TODO
				// return to main profile screen

			} 
			
		} catch (NullPointerException e) {
			System.err.println("** Class Name : Profile");
			System.err.println("   Method Name : getScoreSheet()");
			System.err.println("   try/catch block: NullPointerException");
			System.err.println("**");
			System.err.println(Paths.getProfilePath() + getUserPID() + ".txt does not exist.");

		}
		return uScoreSheet;
	}

	public List<String> getRawScores() {

		String rawScore = " ";
		arrayOfScores = new ArrayList<String>();

		try {
			if (getScoreSheet().exists()) {
				br = new BufferedReader(new FileReader(getScoreSheet()));
				
				while ((rawScore = br.readLine()) != null) {
					arrayOfScores.add(rawScore);
				} 
			} 

			br.close();

		} catch (IOException e) {
			System.err.println("** Class Name : Profile");
			System.err.println("   Method Name : getScores()");
			System.err.println("   try/catch block: IOException");
			System.err.println("**");
			System.err.println(getUserPID() + ".txt read error.");

		} catch (NullPointerException e) {
			System.err.println("** Class Name : Profile");
			System.err.println("   Method Name : getScores()");
			System.err.println("   try/catch block: NullPointerException ");
			System.err.println("**");
			System.err.println(Paths.getProfilePath() + getUserPID() + ".txt does not exist.");
		}

		return arrayOfScores.subList(1, arrayOfScores.size()-1);

	}

	public int getTimesPlayed() {

		return arrayOfScores.size()-1;

	}
	
	public String getPercentage() {
		
		return Double.valueOf(arrayOfScores.get(0)) + " %";
		
	}

	public int getTotalScore() {
		
		int total = 0;

		for (int i = 1; i < arrayOfScores.size(); i++) {
			
			total+=Integer.valueOf(arrayOfScores.get(i));
		}

		return total;

	}

}
